package ECMap

import (
	"git.oschina.net/yangdao/GameLib/Shape"
	"git.oschina.net/yangdao/extlib/data_type"

	"git.oschina.net/yangdao/extlib"

	"github.com/etree"
)

func ECMap_DeCode(mapPath string) *GameMap {
	doc := etree.NewDocument()
	doc.ReadFromFile(mapPath)
	rootElem := doc.SelectElement("ECMap")
	gameMap := &GameMap{
		Items: data_type.Vector(),
		RectShape: Shape.RectShape{
			Width:  extlib.Int(rootElem.SelectAttrValue("Width", "0")) * 100,
			Height: extlib.Int(rootElem.SelectAttrValue("Height", "0")) * 100,
		},
	}

	for _, xmlItem := range rootElem.ChildElements() {
		newItem := &MapItem{}
		gameMap.Items.Conj(newItem)
		switch xmlItem.SelectAttrValue("ShapeType", "-1") {
		case "0":
			newItem.Shape = &Shape.RectShape{
				Width:  extlib.Int(xmlItem.SelectAttrValue("ShapeWidth", "0")),
				Height: extlib.Int(xmlItem.SelectAttrValue("ShapeHeight", "0")),
				X:      extlib.Float32(xmlItem.SelectAttrValue("PosX", "0")),
				Y:      extlib.Float32(xmlItem.SelectAttrValue("PosZ", "0")),
			}
			newItem.Slot = extlib.Int(xmlItem.SelectAttrValue("Data", "0"))

		case "-1":
			panic("错位的形状类型")
		}

	}
	return gameMap
}
